﻿using AutoMapper;
using MediatR;
using T_settlement.API.Application.Command.InvoiceCommand;
using T_settlement.Domain;
using T_settlement.Infrastructure.Interfacec;

namespace T_settlement.API.Application.CommandHandlers.InvoiceCommandHandlers
{
    public class AddInvoiceCommandHandlers:IRequestHandler<AddInvoiceCommand,int>
    {
        private readonly InvoiceRepository invoiceRepository;
        private readonly IMapper mapper;
        public AddInvoiceCommandHandlers(InvoiceRepository invoiceRepository, IMapper mapper)
        {
            this.invoiceRepository = invoiceRepository;
            this.mapper = mapper;
        }

        public async Task<int> Handle(AddInvoiceCommand request, CancellationToken cancellationToken)
        {
            var info  = mapper.Map<Invoice>(request);
            info.InvoiceNumber = "SY" + DateTime.Now.ToString("yyyyMMddss");
            info.CreateBy = "张三";
            info.CreateDate = DateTime.Now;
            return await invoiceRepository.AddAsync(info);
            //var info = new Invoice();
            //info.InvoiceNumber = "SY" + DateTime.Now.ToString("yyyyMMddss");
            //info.Ticket = request.Ticket;    
            //info.InvoiceType = request.InvoiceType;
            //info.InvoiceAmount = request.InvoiceAmount;
            //info.Rate = request.Rate;
            //info.Tax = request.Tax;
            //info.Invoicetime= request.Invoicetime;  
            //info.Remark = request.Remark;
            //info.CreateBy = "张三";
            //info.CreateDate = DateTime.Now;
            //info.AuditState = request.AuditState;
            //info.UpdateBy = null;
            //info.UpdateDate = null;
            //var res= await invoiceRepository.AddAsync(info);
            //return res;
        }
    }
}
